var account={
    init:function(){
        var self=this;
        self.shadeDiv = $("#shade-div");
        self.bindEvents();
        self.loadTree();
    },
    bindEvents:function(){
        var self=this;
//        新增账户输入用户名检验
        $("#validate input").blur(function(){
            //检验输入用户，配合后台
        	var url = ctx+"/account/account!checkUser.xhtml";
        	 var posta ="userCode="+$("#addUserCode").val();
        	 $.post(url, posta,function(result){
        		 	var status=false;
        			if(result.status=="success"){
        				status=true;
        			}
        			var $span=$("#validate input").siblings("span");
                    if(status){
                        $span.addClass("suc").text("");
                    }else{
                        $span.removeClass("suc").text(result.message);
                    }
                    $span.show();
        	   }, "json");
        });
        $("#validate input").focus(function(){
            $(this).siblings("span").removeClass("suc").hide();
        });
//        新增账户
        $("#newAccount").click(function(){
        	$("#addUserCode").val("");
        	//加载部门数据
        	account.ajaxPOST(ctx+"/account/account!getDeptTree.xhtml","",function(data){
        		$.fn.zTree.init($("#treeDemo1"), setting1, data);
        	});
        	//加载菜单功能数据
        	account.ajaxPOST(ctx+"/account/account!getFuncTree.xhtml","",function(data){
        		$.fn.zTree.init($("#treeDemo2"), setting1, data);
        	});
            self.shadeDiv.show();
            $('#addAccount').setPosition().show();
        });
        
        
        //保存
        $("#saveButton").click(function(){
        	var $span=$("#validate input").siblings("span");
        	if($span.text()!=""){
        		alert("请输入正确的用户");
        		return;
        	}
        	var pwd=$("#pwd").val();
        	var surepwd=$("#surepwd").val();
        	if(pwd==""){
        		alert("请输入密码");
        		return;
        	}
        	if(pwd!=surepwd){
        		alert("两次密码不一致，请重新输入");
        		return;
        	}
        	var funcChecked=$.fn.zTree.getZTreeObj("treeDemo2").getCheckedNodes();
        	var funcIds="";//功能菜单选中
        	for(var i=0;i<funcChecked.length;i++){
        		if(i==0){
        			funcIds+=funcChecked[i].id;
        		}else{
        			funcIds+=","+funcChecked[i].id;
        		}
        	}
        	var deptChecked=$.fn.zTree.getZTreeObj("treeDemo1").getCheckedNodes();
        	var deptIds="";//部门菜单选中
        	for(var i=0;i<deptChecked.length;i++){
        		if(i==0){
        			deptIds+=deptChecked[i].id;
        		}else{
        			deptIds+=","+deptChecked[i].id;
        		}
        	}
        	$("#saveForm_deptIds").val(deptIds);
        	$("#saveForm_funcIds").val(funcIds);
        	account.ajaxPOST(ctx+"/account/account!save.xhtml",$("#saveForm").serialize(),function(data){
        		common.refreshPage();
        	});
        	
        });
        
//        编辑账户
        $(".edit").click(function(){
        	var userid=$(this).attr("value");
        	
        	
        	//加载用户数据
        	account.ajaxPOST(ctx+"/account/account!edit.xhtml","userInfo.id="+userid,function(data){
        		$("#editForm_userId").val(data.id);
        		$("#editUserCode").val(data.userCode);
        		$("#editRoleid").val(data.role.id);
        		$("#editRemark").val(data.remark);
        		$("#editpwd").val(data.password);
        	});
        	
        	//加载部门数据
        	account.ajaxPOST(ctx+"/account/account!getDeptTree.xhtml","",function(data){
        		$.fn.zTree.init($("#treeDemo3"), setting1, data);
        		//选中部门
            	account.ajaxPOST(ctx+"/account/account!getDeptTreeIds.xhtml","userInfo.id="+userid,function(data){
            		var treeDemo3=$.fn.zTree.getZTreeObj("treeDemo3");
            		for(var i=0;i<data.length;i++){
                		var node = treeDemo3.getNodeByParam("id",data[i].id);
                		treeDemo3.checkNode(node, true, true);
                	}
            	});
        	});
        	//加载菜单功能数据
        	account.ajaxPOST(ctx+"/account/account!getFuncTree.xhtml","",function(data){
        		$.fn.zTree.init($("#treeDemo4"), setting1, data);
        		//选中菜单
            	account.ajaxPOST(ctx+"/account/account!getFuncTreeIds.xhtml","userInfo.id="+userid,function(data){
            		var treeDemo4=$.fn.zTree.getZTreeObj("treeDemo4");
            		for(var i=0;i<data.length;i++){
                		var node = treeDemo4.getNodeByParam("id",data[i].id);
                		treeDemo4.checkNode(node, true, true);
                	}
            	});
        	});
            self.shadeDiv.show();
            $('#editAccount').setPosition().show();
        });
        
        //修改保存
        $("#editButton").click(function(){
        	var pwd=$("#editpwd").val();
        	if(pwd==""){
        		alert("请输入密码");
        		return;
        	}
        	var deptChecked=$.fn.zTree.getZTreeObj("treeDemo3").getCheckedNodes();
        	var deptIds="";//部门菜单选中
        	for(var i=0;i<deptChecked.length;i++){
        		if(i==0){
        			deptIds+=deptChecked[i].id;
        		}else{
        			deptIds+=","+deptChecked[i].id;
        		}
        	}
        	var funcChecked=$.fn.zTree.getZTreeObj("treeDemo4").getCheckedNodes();
        	var funcIds="";//功能菜单选中
        	for(var i=0;i<funcChecked.length;i++){
        		if(i==0){
        			funcIds+=funcChecked[i].id;
        		}else{
        			funcIds+=","+funcChecked[i].id;
        		}
        	}
        	
        	$("#editForm_deptIds").val(deptIds);
        	$("#editForm_funcIds").val(funcIds);
        	account.ajaxPOST(ctx+"/account/account!update.xhtml",$("#editForm").serialize(),function(data){
        		common.refreshPage();
        	});
        });
        
        
//        删除账户
        $(".del").click(function(){
        	$("#sureDelId").val($(this).attr("value"));
            self.shadeDiv.show();
            $('#warning').setPosition().show().find("p.center").html("确定要删除此账户吗？");
        });
        $("#sureDelButton").click(function(){
        	account.ajaxPOST(ctx+"/account/account!delete.xhtml","userInfo.id="+$("#sureDelId").val(),function(data){
        		common.refreshPage();
        	});
        });
        $("#searchBy").textClean();

    },
    loadTree:function(){
    	
    },
    ajaxGET:function (url, data, fn,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"GET", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    },
    ajaxPOST:function (url, data, fn,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"POST", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    }
};

$(function(){
    account.init();
});